package com.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingnan.mapper.ActiveMapper;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.Apply;
import com.lingnan.mapper.ApplyMapper;
import com.lingnan.service.ApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.vo.ActiveVo;
import com.lingnan.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 报名表 服务实现类
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService {

    @Autowired
    private ApplyMapper applyMapper;

    @Autowired
    private ActiveMapper activeMapper;
    @Override
    public Apply addApplyActive(ActiveVo activeVo) {
        Apply apply = new Apply();
        apply.setActiveId(activeVo.getId());
        apply.setNo(activeVo.getNo());
        LocalDate localDate = LocalDate.now();
        apply.setApplyTime(String.valueOf(localDate));
        apply.setIsFollow(1);
        int insert = applyMapper.insert(apply);
        if (insert>0){
            return apply;
        }
        return null;
    }


    @Override
    public List<Active> findAllByNo(UserVo userVo) {
        ConcurrentHashMap<String, Object> hashMap = new ConcurrentHashMap<>();
        hashMap.put("no", userVo.getNo());
        List<Apply> targets = applyMapper.selectByMap(hashMap);
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < targets.size(); i++) {
            integers.add(targets.get(i).getActiveId());
        }
        List<Active> actives1 = new ArrayList<>();

        integers.stream().forEach(e ->{
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",e);
            map.put("info_type","非通知");
            List<Active> actives = activeMapper.selectByMap(map);
            if (actives.size() > 0){
                Active active = actives.get(0);
                actives1.add(active);
            }
        });
        return actives1;
    }

    @Override
    public List<Active> findJoinedByNo(UserVo userVo) {
        ConcurrentHashMap<String, Object> hashMap = new ConcurrentHashMap<>();
        hashMap.put("no", userVo.getNo());
        List<Apply> targets = applyMapper.selectByMap(hashMap);
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < targets.size(); i++) {
            integers.add(targets.get(i).getActiveId());
        }
        List<Active> actives1 = new ArrayList<>();

        integers.stream().forEach(e ->{
            QueryWrapper<Active> wrapper = new QueryWrapper<>();
            wrapper.eq("id",e);
            wrapper.eq("info_type","非通知");
            LocalDate now = LocalDate.now();
            String endTime = now.format(DateTimeFormatter.ISO_DATE); //2022-03-26
            //小于今天
            wrapper.lt("end_time",endTime);
            List<Active> list = activeMapper.selectList(wrapper);
            if (list.size() > 0){
                Active active = list.get(0);
                actives1.add(active);
            }
        });
        return actives1;
    }
}
